import axios from 'axios'
import { notification } from 'ant-design-vue'
import { useStorage } from '@vueuse/core'

function Logout() {
    const Token = useStorage('token', '', localStorage);
    const Permission = useStorage('permission', '', localStorage);
    const NickName = useStorage('NickName', '', localStorage);
    Token.value = null;
    Permission.value = null;
    NickName.value = null;
    window.location.reload();
}
const Event = function (options: any) {
    const Token = useStorage('token', '', localStorage);
    var _url = window.WIN_C.BaseUrl + options.url;
    var _params = JSON.parse(JSON.stringify(options.params));
    _url = _url.replace(/\{\w+\}/g, function (i: any) {
        var _src = i.replace('{', '').replace('}', '');
        if (_params.hasOwnProperty(_src)) {
            var _value = _params[_src];
            delete _params[_src];
            return _value;
        }
    });

    let ajax = axios({
        method: options.type,
        url: _url,
        params: _params,
        headers: {
            token: Token.value,
            ...options.header
        },
        data: options.data
    }).then(function (res) {
        return res.data;
    }).catch(function (error) {
        if (error.response.status == 400) {
            notification.error({ message: 'Service Error 400', description: '参数错误，请检查参数信息' });
        } else if (error.response.status == 403) {
            notification.error({ message: 'Service Error 403', description: '权限错误 ' + (error.response.data.message ? (':' + error.response.data.message) : '') });
            setTimeout(() => {
                Logout();
            }, 2000);
        } else if (error.response.status == 404) {
            notification.error({ message: 'Service Error 404', description: '未找到接口，请检查链接地址' });
        } else if (error.response.status == 500) {
            notification.error({ message: 'Service Error 500', description: '服务器出现错误 : ' + (error.response.data.message ? error.response.data.message : '') });
        } else if (error.response.status == 502) {
            notification.error({ message: 'Service Error 502', description: '服务器响应出现错误' });
        } else {
            notification.error({ message: 'Service Error' + error.response.status, description: error.response.data.message });
        }
        return Promise.reject(error) // 返回接口返回的错误信息
    });
    return ajax;
}

export default {

    /**
     * apis 接口调用参数
     *
     * @param {String} url  链接地址
     * @param {Object} params Url 传参对象
     * @param {Object} data PostData 传参对象
     * @return {axios} 接口异步对象
     */
    post(url: string, params: any, data: any, header?: any) {
        var _data: any = {};
        _data.type = 'post';
        _data.data = data || {};
        _data.params = params;
        _data.url = url;
        _data.header = header || {};
        return Event(_data)
    },
    get(url: string, params: any, data: any, header?: any) {
        var _data: any = {};
        _data.type = 'get';
        _data.data = data || {};
        _data.params = params;
        _data.url = url;
        _data.header = header || {};
        return Event(_data)
    },
    put(url: string, params: any, data: any, header?: any) {
        var _data: any = {};
        _data.type = 'put';
        _data.data = data || {};
        _data.params = params;
        _data.url = url;
        _data.header = header || {};
        return Event(_data)
    },
    delete(url: string, params: any, data: any, header?: any) {
        var _data: any = {};
        _data.type = 'delete';
        _data.data = data || {};
        _data.params = params;
        _data.url = url;
        _data.header = header || {};
        return Event(_data)
    }
}


